package com.ruicar.afs.cloud.basic.creditchange.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.api.dto.CustContactVO;
import com.ruicar.afs.cloud.basic.common.entity.*;
import com.ruicar.afs.cloud.basic.common.service.*;
import com.ruicar.afs.cloud.basic.creditchange.condition.OldCustInfoCondition;
import com.ruicar.afs.cloud.basic.creditchange.vo.BankCardChangeVO;
import com.ruicar.afs.cloud.basic.creditchange.vo.CustRoleInfoVO;
import com.ruicar.afs.cloud.basic.creditchange.vo.OldCustInfoVO;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.PersonRoleTypeEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.ProductAtomEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author chuke
 * @create 2020/6/29 10:41
 * 查询合同案件列表
 */
@Slf4j
@RestController
@RequestMapping("/oldCustInfo")
@AllArgsConstructor
public class OldCustInfoController {
    private final BasicCustBaseInfoService baseInfoService;
    private final BasicCustAddressDetailsService addressDetailsService;
    private final BasicCustContactDetailsService contactDetailsService;
    private final BasicCustBaseInfoService basicCustBaseInfoService;
    private final BasicBankCardService bankCardService;
    private final BasicMainInfoService basicMainInfoService;
    private final BasicContractCustRelationService custRelationService;
    private final BasicProductParamService basicProductParamService;


    @ApiOperation(value = "查询合同客户的原联系方式和地址")
    @PostMapping("/findContractOldContact")
    public IResponse findBasicMainInfoList(@RequestParam("contractNo") String contractNo,
                                           @RequestParam(value = "custRole", required = false) PersonRoleTypeEnum custRole) {


        CustContactVO customerInfoVO = new CustContactVO();
        /** TODO CK 默认担保人为一个人，后续进行改进*/
        //查询客户信息
        BasicContractCustRelation custRelation = custRelationService.getOne(Wrappers.<BasicContractCustRelation>lambdaQuery()
                .eq(BasicContractCustRelation::getContractNo, contractNo)
                .eq(BasicContractCustRelation::getCustRole, custRole));
        if (EmptyUtils.isEmpty(custRelation)) {
            return new IResponse().fail("没有查询到客户信息");
        }
        BasicCustBaseInfo basicCustBaseInfo = basicCustBaseInfoService.getOne(Wrappers.<BasicCustBaseInfo>lambdaQuery()
                .eq(BasicCustBaseInfo::getCustNo, custRelation.getCustNo()));

        if (basicCustBaseInfo == null) {
            return new IResponse().fail("没有查询到客户信息");
        }

        customerInfoVO.setCustName(basicCustBaseInfo.getCustName());
        customerInfoVO.setCustNo(basicCustBaseInfo.getCustNo());
        //根据客户号获取客户的原地址信息
        List<BasicCustAddressDetails> custAddressDetailsList = addressDetailsService.list(Wrappers.<BasicCustAddressDetails>lambdaQuery()
                .eq(StringUtils.isNotEmpty(custRelation.getCustNo()), BasicCustAddressDetails::getCustNo, custRelation.getCustNo()));
        //根据客户号获取客户的原联系信息
        List<BasicCustContactDetails> custContactDetailsList = contactDetailsService.list(Wrappers.<BasicCustContactDetails>lambdaQuery()
                .eq(StringUtils.isNotEmpty(custRelation.getCustNo()), BasicCustContactDetails::getCustNo, custRelation.getCustNo()));
        for (BasicCustAddressDetails custAddressDetails : custAddressDetailsList) {
            switch (custAddressDetails.getAddressType()) {
                case LIVE:
                    String[] oldLiveAddress = {custAddressDetails.getProvince(), custAddressDetails.getCity(), custAddressDetails.getCounty(), custAddressDetails.getStreet()};
                    customerInfoVO.setOldLiveAddress(oldLiveAddress);
                    customerInfoVO.setOldLiveAddressDetail(custAddressDetails.getAddress());
                    break;
                case WORK:
                    String[] oldWorkAddress = {custAddressDetails.getProvince(), custAddressDetails.getCity(), custAddressDetails.getCounty(), custAddressDetails.getStreet()};
                    customerInfoVO.setOldWorkAddress(oldWorkAddress);
                    customerInfoVO.setOldWorkAddressDetail(custAddressDetails.getAddress());
                    break;
                case HOME:
                    String[] oldHomeAddress = {custAddressDetails.getProvince(), custAddressDetails.getCity(), custAddressDetails.getCounty(), custAddressDetails.getStreet()};
                    customerInfoVO.setOldHomeAddress(oldHomeAddress);
                    customerInfoVO.setOldHomeAddressDetail(custAddressDetails.getAddress());
                    break;
                case MAILING:
                    String[] oldMailingAddress = {custAddressDetails.getProvince(), custAddressDetails.getCity(), custAddressDetails.getCounty(), custAddressDetails.getStreet()};
                    customerInfoVO.setOldMailingAddress(oldMailingAddress);
                    customerInfoVO.setOldMailingAddressDetail(custAddressDetails.getAddress());
                    break;
            }
        }
        for (BasicCustContactDetails contactDetails : custContactDetailsList) {
            switch (contactDetails.getContactType()) {
                case PHONE:
                    customerInfoVO.setOldPhone(contactDetails.getContactNo());
                    break;
                case WORK_PHONE:
                    customerInfoVO.setOldWorkPhone(contactDetails.getContactNo());
                    break;
                case HOME_CONTACT:
                    customerInfoVO.setOldHomeContact(contactDetails.getContactNo());
                    break;
                case OTHER_CONTACT:
                    customerInfoVO.setOldOtherContact(contactDetails.getContactNo());
                    break;
            }
        }
        return new IResponse<CustContactVO>().setData(customerInfoVO);
    }

    @ApiOperation(value = "查询合同客户的原身份信息")
    @PostMapping("/findOldCustInfo")
    public IResponse findOldCustInfo(@RequestBody OldCustInfoCondition oldCondition) {
        // TODO CK 默认当前担保人为一个人，后续更改
        OldCustInfoVO custBaseInfo = new OldCustInfoVO();
//        List<OldCustInfoVO> custInfoVOS = new ArrayList<>();
        BasicMainInfo basicMainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>lambdaQuery()
                .eq(BasicMainInfo::getContractNo, oldCondition.getContractNo()));
        //根据合同号和案件角色查询客户号
      /*  List<BasicContractCustRelation> custRelations = custRelationService.list(Wrappers.<BasicContractCustRelation>lambdaQuery()
                .eq(BasicContractCustRelation::getContractNo, oldCondition.getContractNo())
                .eq(BasicContractCustRelation::getCustRole, oldCondition.getChangeSubject()));*/
//        for (BasicContractCustRelation custRelation : custRelations) {

        BasicContractCustRelation custRelation = custRelationService.getOne(Wrappers.<BasicContractCustRelation>lambdaQuery()
                .eq(BasicContractCustRelation::getContractNo, oldCondition.getContractNo())
                .eq(BasicContractCustRelation::getCustRole, oldCondition.getChangeSubject()));
        //根据查来的客户号查询客户原身份信息
        BasicCustBaseInfo custInfo = baseInfoService.getOne(Wrappers.<BasicCustBaseInfo>lambdaQuery()
                .eq(BasicCustBaseInfo::getCustNo, custRelation.getCustNo()));

        //主借人
        BasicContractCustRelation mainPerson = custRelationService.getOne(Wrappers.<BasicContractCustRelation>lambdaQuery()
                .eq(BasicContractCustRelation::getContractNo, oldCondition.getContractNo())
                .eq(BasicContractCustRelation::getCustRole, "MAIN_PERSON"));
        BasicCustBaseInfo mainName = baseInfoService.getOne(Wrappers.<BasicCustBaseInfo>lambdaQuery()
                .eq(BasicCustBaseInfo::getCustNo, mainPerson.getCustNo()));

        custBaseInfo.setOldName(custInfo.getCustName());
        custBaseInfo.setCustNo(custInfo.getCustNo());
        custBaseInfo.setCustName(mainName.getCustName());
        custBaseInfo.setCertType(custInfo.getCertType());
        custBaseInfo.setCertNo(custInfo.getCertNo());
        custBaseInfo.setCertStartDate(custInfo.getCertStartDate());
        custBaseInfo.setCertEndDate(custInfo.getCertEndDate());
        custBaseInfo.setCustNo(custInfo.getCustNo());
        custBaseInfo.setCloseDate(basicMainInfo.getEndDate() != null ? basicMainInfo.getEndDate() : null);//合同关闭时间
        BasicBankCard bankCard = bankCardService.getOne(Wrappers.<BasicBankCard>lambdaQuery()
                .eq(BasicBankCard::getContractNo, oldCondition.getContractNo())
        );
        if (bankCard != null) {
            custBaseInfo.setBankCertNo(bankCard.getCertNo());
        }
        //查询身份信息验证时间
        BasicProductParam basicProductParam = basicProductParamService.getOne(Wrappers.<BasicProductParam>lambdaQuery()
                .eq(BasicProductParam::getContractNo, oldCondition.getContractNo())
                .eq(BasicProductParam::getAtomKey, ProductAtomEnum.IS_BANK_CARD_CHANGE_DAYS));
        if (basicProductParam == null) { //若没有规定身份证过期时间，则默认为20天
            custBaseInfo.setIdCardDays(String.valueOf(20));
        } else {
            custBaseInfo.setIdCardDays(basicProductParam.getAtomValue());
        }
//            custInfoVOS.add(custBaseInfo);
//        }
        return new IResponse<OldCustInfoVO>().setData(custBaseInfo);
    }


    @ApiOperation(value = "查询合同客户的原银行卡信息")
    @PostMapping("/findOldBankInfo")
    public IResponse findOldBankInfo(@RequestBody BasicBankCard bankCard) {
        BankCardChangeVO vo = new BankCardChangeVO();
        //根据合同号查询客户的银行卡信息
        BasicBankCard basicBankCard = bankCardService.getOne(Wrappers.<BasicBankCard>lambdaQuery()
                .eq(BasicBankCard::getContractNo, bankCard.getContractNo()));
        if (basicBankCard == null) {
            throw new AfsBaseException("该合同未绑定银行卡");
        }
        BasicContractCustRelation relation = custRelationService.getOne(Wrappers.<BasicContractCustRelation>lambdaQuery()
                .eq(BasicContractCustRelation::getCustRole,PersonRoleTypeEnum.MAIN_PERSON)
                .eq(BasicContractCustRelation::getContractNo,bankCard.getContractNo()));
        BasicMainInfo mainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>lambdaQuery()
                .eq(BasicMainInfo::getContractNo, bankCard.getContractNo()));
        BasicCustBaseInfo custBaseInfo = basicCustBaseInfoService.getOne(Wrappers.<BasicCustBaseInfo>lambdaQuery()
                .eq(BasicCustBaseInfo::getCertNo, basicBankCard.getCertNo()));
        BasicCustBaseInfo custName = basicCustBaseInfoService.getOne(Wrappers.<BasicCustBaseInfo>lambdaQuery()
                .eq(BasicCustBaseInfo::getCustNo, relation.getCustNo()));
        basicBankCard.setId(null);
        if (custBaseInfo != null) {
            vo.setContactNo(StringUtils.isNotEmpty(custBaseInfo.getPhone()) ? custBaseInfo.getPhone() : null);
        }
        //查询银行卡信息
        vo.setCustName(StringUtils.isNotEmpty(custName.getCustName()) ? custName.getCustName() : null);
        vo.setStartDate((mainInfo.getStartDate() != null) ? mainInfo.getStartDate() : null);
        vo.setAccountName(StringUtils.isNotEmpty(basicBankCard.getAccountName()) ? basicBankCard.getAccountName() : null);
        vo.setAccountNo(StringUtils.isNotEmpty(basicBankCard.getAccountNo()) ? basicBankCard.getAccountNo() : null);
        vo.setBankCode(StringUtils.isNotEmpty(basicBankCard.getBankCode()) ? basicBankCard.getBankCode() : null);
//        vo.setOldCertNo(StringUtils.isNotEmpty(basicBankCard.getCertNo()) ? basicBankCard.getCertNo() : null);
////        vo.setBankBranch(StringUtils.isNotEmpty(basicBankCard.getBankBranch()) ? basicBankCard.getBankBranch() : null);
        vo.setCloseDate(EmptyUtils.isNotEmpty(mainInfo.getEndDate()) ? mainInfo.getEndDate() : null);//合同到期时间
        vo.setProvince(basicBankCard.getOpenProvince());
        vo.setCity(basicBankCard.getOpenCity());
        vo.setCertStartDate(custBaseInfo.getCertStartDate());
        vo.setCertEndDate(custBaseInfo.getCertEndDate());
        List<BasicProductParam> basicProductParam = basicProductParamService.list(Wrappers.<BasicProductParam>lambdaQuery()
                .eq(BasicProductParam::getContractNo, bankCard.getContractNo())
                .eq(BasicProductParam::getAtomKey, ProductAtomEnum.IS_BANK_CARD_CHANGE_DAYS));
        if (basicProductParam.size() == 0) { //若没有规定身份证过期时间，则默认为20天
            vo.setIdCardDays(String.valueOf(20));
        } else {
            vo.setIdCardDays(basicProductParam.get(0).getAtomValue());
        }
        return new IResponse<BankCardChangeVO>().setData(vo);
    }

    @ApiOperation(value = "查询合同的主共借人")
    @PostMapping("/findContractPerson")
    public IResponse findContractPerson(@RequestBody BasicBankCard bankCard) {
        List custNos = new ArrayList();
        List<CustRoleInfoVO> custRoleInfoVOList = new ArrayList<>();
        List<BasicContractCustRelation> custRelations = custRelationService.list(Wrappers.<BasicContractCustRelation>lambdaQuery()
                .eq(BasicContractCustRelation::getContractNo, bankCard.getContractNo())
                .notIn(BasicContractCustRelation::getCustRole, "GUARANTEE_PERSON"));
        for (BasicContractCustRelation custRelation : custRelations) {
                custNos.add(custRelation.getCustNo());
        }
        List<BasicCustBaseInfo> custBaseInfo = baseInfoService.list(Wrappers.<BasicCustBaseInfo>lambdaQuery()
                .in(BasicCustBaseInfo::getCustNo, custNos));
        for (BasicCustBaseInfo baseInfo : custBaseInfo) {
            CustRoleInfoVO vo = new CustRoleInfoVO();
            vo.setCertNo(baseInfo.getCertNo());
            vo.setCustName(baseInfo.getCustName());
            custRoleInfoVOList.add(vo);
        }
        return new IResponse().setData(custRoleInfoVOList);
    }

}